
#include "namespace.h"

#include <QObject>
#include <QString>

#include <sstream>

NameSpace::NameSpace()
{
}

NameSpace::~NameSpace()
{
}

void NameSpace::unregisterName(const std::string &name)
{
    m_names.erase(name);
}

bool NameSpace::registerName(const std::string &name)
{
    if(m_names.find(name) != m_names.end())
        return false;

    m_names.insert(name);
    return true;
}

bool NameSpace::isNameRegistered(const std::string &name)
{
    return (m_names.find(name) != m_names.end());
}

const std::string NameSpace::generateName(
    const std::string &name
,   const std::string &ignoreName)
{
    std::string generatedName;
    std::string proposedName = name;
    if(proposedName.empty())
        proposedName = QObject::tr("RenameMe").toStdString();

    unsigned int i = m_names.find(name) == m_names.end() ? 0 : 1;
    do
    {
        std::ostringstream temp;
        if(i == 0)
            temp << proposedName;
        else
            temp << proposedName << ++i;

        generatedName = temp.str();

    } while(m_names.find(generatedName) != m_names.end() && ignoreName != generatedName);

    return generatedName;
}
